//
//  WJSpacingView.swift
//  DZMeBookRead
//
//  Created by fox on 2017/10/13.
//  Copyright © 2017年 DZM. All rights reserved.
//

import UIKit

class WJSpacingView: UIView {

    var tempSender = UIButton()
    var changeSpacing:((_ sender:UIButton) ->Void)?
    var btns:[UIButton] = []
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        // 126
        // 64
        let btnW = 64
        let btnH = 32
        let spacing = (ScreenWidth - 210) / 4
        for index in 0...btns.count - 1 {
            let btn = btns[index]
            btn.frame = CGRect.init(x:Int(spacing) + (index * (btnW + Int(spacing))), y: 0, width: btnW, height: btnH)
        }

    }
   func setupUI() {
    for index in 0...2 {
        let btn = UIButton()

        addSubview(btn)
        btn.tag = index
        btns.append(btn)
        btn.addTarget(self, action: #selector(clickSpacing(sender:)), for: .touchUpInside)
       let norStr = String.init(format: "spacing%d", index + 3)
       let selectStr = String.init(format: "spacing_rea%d", index + 3)


        btn.setImage(UIImage.init(named: norStr), for: .normal)
        btn.setImage(UIImage.init(named: selectStr), for: .selected)

        if btn.tag == DZMReadConfigure.shared().paragraphStyleIndex {
            btn.isSelected = true
        }
    }
  }

    func clickSpacing(sender:UIButton) {
        changeSpacing?(sender)
        tempSender.isSelected = false
        sender.isSelected = true
        tempSender = sender
    }
}
